<template>
  <div class="dashboard-container">
    <div class="filter-container">
      <el-row :gutter="10" style="width: 100%">
        <el-col :span="4">
          <el-input
            v-model="listQuery.username"
            placeholder="管理员名称"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="listQuery.mobile"
            placeholder="管理员手机号"
            class="input-with-select"
            @keypress.enter.native="reloadList"
          />
        </el-col>
        <el-col :span="4">
          <el-select
            v-model="listQuery.status"
            style="width: 100%"
            placeholder="状态"
            clearable
            @change="reloadList"
          >
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-col>
        <el-col :span="6">
          <div>
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="reloadList"
            >搜索</el-button>
            <el-button
              class="filter-item"
              style="margin-left: 10px;"
              icon="el-icon-edit"
              type="primary"
              @click="addNews"
            >
              添加
            </el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div style="padding: 0 20px">
      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        height="calc(100vh - 200px)"
        style="width: 100%"
        size="mini"
        border
        tooltip-effect="dark"
        :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      >
        <el-table-column label="名称" prop="username" min-width="150" />
        <el-table-column label="手机号" prop="context" width="150">
          <template slot-scope="{ row }">
            <div>
              <span style="margin-right: 5px">+{{ row.area_code }}</span>
              <span>{{ row.mobile }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" width="150">
          <template slot-scope="{ row }">
            <el-switch
              v-model="row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="checkStatus(row.id, row.status)"
            />
          </template>
        </el-table-column>

        <el-table-column label="创建时间" prop="add_time" width="150" />

        <el-table-column label="操作" prop="status" width="130">
          <template slot-scope="{ row }">
            <el-button
              class="filter-item"
              type="primary"
              @click="openPass(row)"
            >修改密码</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <el-dialog
      :title="'修改'+row.username+'的密码'"
      :visible.sync="dialogVisible"
      width="400px"
    >
      <div class="pass">
        <el-input v-model="password" style="flex:2" placeholder="请输入密码" show-password />
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitPass">确 定</el-button>
      </span>
    </el-dialog>

    <Detial :detial="detial" :status="status" :visible="visible" @update="closeDraw" @reload="reloadList" />
  </div>
</template>

<script>
import { listAdmin, updateStatusAdmin, resettingPassword } from '@/api/admin/index'

import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Detial from './components/adminDetial.vue'

export default {
  name: 'Dashboard',
  components: {
    Pagination,
    Detial
  },
  data() {
    return {
      dialogVisible: false,
      data_arr: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        username: '',
        code: '',
        add_time: []
      },
      visible: false,
      status: 1,
      id: '',
      detial: {},
      message: '',
      row: {},
      password: ''
    }
  },
  computed: {},
  created() {
    this.getList()
  },
  methods: {
    closeDraw() {
      this.visible = false
    },
    addNews() {
      this.status = 1
      this.visible = true
    },
    edit(row) {
      this.detial = { ...row }
      this.status = 2
      this.visible = true
    },
    // delet(row) {
    //   this.$confirm('确认删除该航班, 是否继续?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     delFlight({
    //       id: row.id
    //     }).then(res => {
    //       this.$message({
    //         type: 'success',
    //         message: '删除成功!'
    //       })
    //       this.getList()
    //     })
    //   }).catch(() => {

    //   })
    // },
    reloadList() {
      this.listQuery.page = 1
      this.visible = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      listAdmin(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    checkStatus(id, status) {
      updateStatusAdmin({ id: id, status: status }).then((response) => {})
    },
    openPass(row) {
      this.row = row
      this.password = ''
      this.dialogVisible = true
    },
    submitPass() {
      resettingPassword({
        'id': this.row.id,
        'password': this.password
      }).then((response) => {
        this.$message.success('修改成功')
        this.dialogVisible = false
        this.getList()
      })
    }
  }
}
</script>
<style scoped>
.filter-container {
  padding: 20px 20px 10px;
}

.upload-demo {
  margin: 20px 0 10px;
}
.pass{

  padding: 20px;
}
</style>
